package DataServer

import (
	"gitee.com/dark.H/DataDash/in"
	"github.com/gorilla/websocket"
	// self "gitee.com/dark.H/DataViewer/DataServer"
)

func RenderFromNowPanel(key string) (edges in.Datas) {
	points := NowPanelNodes.CaculatePoints(NowPanelEdge, 10000, in.Laplacian)
	for fromID := range NowPanelNodes {
		if _, exists := points[fromID]; !exists {
			continue
		}
		if toID, ok := NowPanelEdge[fromID]; ok {

			in.ColorL("from", fromID, "to", toID)
			data := NowPanelNodes[fromID]["data"].(in.G)
			if key != "" {
				edges = append(edges, in.G{
					"from":  fromID,
					"to":    toID,
					"fromP": points[fromID],
					"toP":   points[toID],
					"label": data[key],
				})
			} else {
				edges = append(edges, in.G{
					"from":  fromID,
					"to":    toID,
					"fromP": points[fromID],
					"toP":   points[toID],
					"label": NowEdgeConnectValue[fromID][toID],
				})
			}

		} else {
			edges = append(edges, in.G{
				"fromP": points[fromID],
			})
		}

	}
	return edges
}

func RenderOnlyEdges(edges in.Edges, nodes in.NodeView) (outEdges in.Datas) {

	outEdges = edges.ToDatas(nodes, NowEdgeConnectValue)
	return
}

func OperReply(c *websocket.Conn, mt int, oper string, datas in.Datas) {
	allcount := len(datas)
	st := DisplaySettings.Page * DisplaySettings.PageLen
	if st > allcount {
		var tmp in.Datas
		c.WriteMessage(mt, in.ToJson(in.G{
			"op":   oper,
			"data": tmp,
		}))
	} else {
		et := st + DisplaySettings.PageLen
		if et >= allcount {
			et = allcount
		}
		c.WriteMessage(mt, in.ToJson(in.G{
			"op":   oper,
			"data": datas[st:et],
		}))
	}

}

func queueToReply(c *websocket.Conn, mt int) {
	for {
		data := <-WaitData
		OperReply(c, mt, data.Op, data.Data)
	}
}
